package cn.jxau.qt.core.factory;

import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;

public class BasicFactory {
	private BasicFactory() {
	}

	private static BasicFactory factory = new BasicFactory();
	private static Properties pop = new Properties();
	static {
		try {
			pop.load(new FileReader(BasicFactory.class.getClassLoader()
					.getResource("config.properties").getPath()));
		} catch (IOException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	public static BasicFactory getFactory() {
		return factory;
	}

	@SuppressWarnings("unchecked")
	public <T> T getInstance(Class<T> clazz) {
		// 获得接口名
		String interfaceName = clazz.getSimpleName();
		// 通过properties获取实现接口的类名
		String ImplName = pop.getProperty(interfaceName);

		try {
			return (T) Class.forName(ImplName).newInstance();
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}

	}
}
